#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define filename "log.txt"

int main()
{
    //close(0);
    //int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); 
    int fd = open(filename, O_WRONLY|O_CREAT|O_APPEND, 0666); 
    if(fd < 0)
    {
        perror("open");
        return 1;
    }
    

    dup2(fd, 1);
    
    printf("fd: %d\n", fd);
    printf("hello printf\n");
    fprintf(stdout, "hello fprintf\n");


    //char inbuffer[1024];
    //int s = read(0, inbuffer, sizeof(inbuffer)-1);
    //if(s > 0)
    //{
    //    inbuffer[s] = '\0';
    //    printf("echo %s\n", inbuffer);
    //}
    //printf("fd: %d\n", fd);
    
    //dup2(fd, 1);
    //close(fd);
    //
    //const char* msg = "hello linux\n";
    //int cnt = 5;
    //while(cnt)
    //{
    //    write(1, msg, strlen(msg));
    //    cnt--;
    //}

    //close(fd);
    return 0;
}
